package main

import (
	"fmt"
	. "nowcoder/common"
)

/**
 *
 * @param root TreeNode类
 * @return int整型二维数组
 */
func levelOrder(root *TreeNode) [][]int {
	// write code here
	result := make([][]int, 0, 10)
	Traversal(root, 0, &result)
	return result
}

func Traversal(node *TreeNode, depth int, result *[][]int) {
	if node == nil {
		return
	}

	if len(*result) <= depth {
		*result = append(*result, []int{})
	}
	(*result)[depth] = append((*result)[depth], node.Val)
	Traversal(node.Left, depth+1, result)
	Traversal(node.Right, depth+1, result)

}

func main() {
	root := &TreeNode{
		Val: 1,
		Left: &TreeNode{
			Val: 2,
			Left: &TreeNode{
				Val:   4,
				Left:  nil,
				Right: nil,
			},
			Right: &TreeNode{
				Val:   5,
				Left:  nil,
				Right: nil,
			},
		},
		Right: &TreeNode{
			Val: 3,
			Left: &TreeNode{
				Val:   6,
				Left:  nil,
				Right: nil,
			},
			Right: &TreeNode{
				Val:   7,
				Left:  nil,
				Right: nil,
			},
		},
	}

	result := levelOrder(root)
	fmt.Println(result)
}
